package inspectedProjects;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.text.Position;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import java.util.Enumeration; 
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/** 
A basic GUI Program on Java
We use this just to demonstrate the "Inspect element" tool
*/
@SuppressWarnings("serial")
public class GUIExample extends Applet implements MouseListener {
	
	// GUI part
	// Initialize all swing objects.
    JFrame f = new JFrame("Simple GUI"); //create Frame
    JFrame f1 = new JFrame("Copy Frame");
    JPanel pnlEast = new JPanel(); // East quadrant
    JPanel pnlWest = new JPanel(); // West quadrant
    JPanel pnlNorth = new JPanel(); // North quadrant
    JPanel pnlSouth = new JPanel();
    // New Label
    JLabel lblText = new JLabel("Simple GUI Form");
    // New Button 
    JButton btnBrowse = new JButton("Browse File");
    JButton btnInspect = new JButton("Show results");
    
    JRadioButton rbtn1 = new JRadioButton("Radio Button1");
    
    // New textField
    JTextField txtField = new JTextField(20);
    
    // Menu
    JMenuBar mb = new JMenuBar(); // Menubar
    JMenu mnuFile = new JMenu(); // File Entry on Menu bar
    JMenuItem mnuItemQuit = new JMenuItem(); // Quit sub item
    JMenu mnuHelp = new JMenu(); // Help Menu entry
    JMenuItem mnuItemAbout = new JMenuItem(); // About Entry

    //Class loaded location
    String fileLocation;
    
    private DefaultMutableTreeNode m_rootNode; 
   
	public void createFrame(){
		JFrame test = new JFrame();
		JPanel pan1 = new JPanel();
		test.getContentPane().add(pan1);
		test.setVisible(true);
		
	}
  
	public class MouseListener extends MouseInputAdapter {
    	
        public void mouseClicked(MouseEvent me) {
            int screenX = me.getXOnScreen();
            int screenY = me.getYOnScreen();
            int X = me.getX();
            int Y = me.getY();
            
            System.out.println("screen(X,Y) = " + screenX + "," + screenY + " ---- frame(X,Y) = "+ X + "," + Y);

          }
    }

    public void mousePosition (){
    	
    	 	while(true){
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			System.out.println("("+MouseInfo.getPointerInfo().getLocation().x 
					+", "+MouseInfo.getPointerInfo().getLocation().y+")");
		}
    }
    
	MouseListener myListener = new MouseListener();	
    /** Constructor for the GUI */

    @SuppressWarnings({ "unchecked", "deprecation" })
	public GUIExample(){
		
    	// Set menubar
        f.setJMenuBar(mb);
		//Build Menus
        mnuFile.add(mnuItemQuit);  // Create Quit line
        mnuHelp.add(mnuItemAbout); // Create About line
        mb.add(mnuFile);        // Add Menu items to form
        mb.add(mnuHelp);

        // Add UI Elements
        pnlNorth.add(lblText);
        pnlEast.add(btnBrowse);
        pnlWest.add(txtField);
        pnlSouth.add(btnInspect);
        pnlSouth.add(rbtn1);
 		txtField.setEditable(false);
        // Setup Main Frame
 		

        f.getContentPane().setLayout(new BorderLayout());
        f.getContentPane().add(pnlEast, BorderLayout.EAST);
		f.getContentPane().add(pnlWest, BorderLayout.WEST);
		f.getContentPane().add(pnlNorth, BorderLayout.NORTH);
		f.getContentPane().add(pnlSouth, BorderLayout.SOUTH);
		

	    ///////////////////////////////////////////////////////////////////////////
	    ///////////////////////////////////////////////////////////////////////////
	    ///////////////////////////////////////////////////////////////////////////
		
		//Browse dialog form on button click
		btnBrowse.addActionListener(new ActionListener()
		{
		     public void actionPerformed(ActionEvent e)
		     {
		         //Open dialog form
		    	String filename = File.separator+"tmp"; 
				JFileChooser fc = new JFileChooser(new File(filename));
				fc.addChoosableFileFilter(new MyFilter());
				fc.showOpenDialog(f);
				
			      int rVal = fc.showOpenDialog(GUIExample.this);
			      if (rVal == JFileChooser.APPROVE_OPTION) {
			    	  txtField.setText(fc.getSelectedFile().toString());
			    	  fileLocation = fc.getSelectedFile().toString();
			    	  System.out.println(fileLocation);
			    	  //fileLocation = fc.getCurrentDirectory().toString();
			    	  //System.out.println(fileLocation);
			      }

		     }
		});

		btnInspect.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
		     {
		    	 //Load and Run the external chosen project
				//GUI f = new GUI();
				//f.launchFrame();
				//GUIProject.main(null);
				
				GUIProject pr = new GUIProject();
				pr.launchFrame();
		     }
		});
	
    }
 
    class MyFilter extends javax.swing.filechooser.FileFilter {
    	public boolean accept(File file) {
    		String filename = file.getName();
    		return filename.endsWith(".java");
    		}
    		public String getDescription() {
    			return "*.java";
    		}
    }
    

	
    public void launchFrame(){
        // Display Frame
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.pack(); //Adjusts panel to components for display
        f.setVisible(true);

    }
    
    public static void main(String args[]) throws AWTException{
        GUIExample gui = new GUIExample();
        gui.launchFrame();
        //gui.mousePosition();
        
    }
   
    	
    ///////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////
    ///////////////////////////////////////////////////////////////////////////
	
	//Run external projects
	private static boolean isWindows() {
        String os = System.getProperty("os.name");
        if (os == null) {
                throw new IllegalStateException("os.name");
        }
        os = os.toLowerCase();
        return os.startsWith("windows");
    }

    public static File getJreExecutable() throws FileNotFoundException {
        String jreDirectory = System.getProperty("java.home");
        if (jreDirectory == null) {
                throw new IllegalStateException("java.home");
        }
        File exe;
        if (isWindows()) {
                exe = new File(jreDirectory, "bin/java.exe");
        } else {
                exe = new File(jreDirectory, "bin/java");
        }
        if (!exe.isFile()) {
                throw new FileNotFoundException(exe.toString());
        }
        return exe;
    }

    public static int launch(ArrayList<String> cmdarray) throws IOException,
                InterruptedException {
        byte[] buffer = new byte[1024];

        ProcessBuilder processBuilder = new ProcessBuilder(cmdarray);
        processBuilder.redirectErrorStream(true);
        Process process = processBuilder.start();
        InputStream in = process.getInputStream();
        while (true) {
                int r = in.read(buffer);
                if (r <= 0) {
                        break;
                }
                System.out.write(buffer, 0, r);
        }
        return process.waitFor();
    }



	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}



	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	class GUI{
		// Initialize all swing objects.
	    private JFrame f1 = new JFrame("Basic GUI"); //create Frame
	    private JPanel pnlNorth = new JPanel(); // North quadrant 
	    private JPanel pnlSouth = new JPanel(); // South quadrant
	    private JPanel pnlEast = new JPanel(); // East quadrant
	    private JPanel pnlWest = new JPanel(); // West quadrant
	    private JPanel pnlCenter = new JPanel(); // Center quadrant

		// Buttons some there is something to put in the panels
	    private JButton btnNorth = new JButton("North");
	    private JButton btnSouth = new JButton("South");
	    private JButton btnEast = new JButton("East");
	    private JButton btnWest = new JButton("West");
	    private JButton btnCenter = new JButton("Center");
	    
	    private JTextField txt1 = new JTextField("Test 1111");

	    // Menu
	    private JMenuBar mb = new JMenuBar(); // Menubar
	    private JMenu mnuFile = new JMenu("File"); // File Entry on Menu bar
	    private JMenuItem mnuItemQuit = new JMenuItem("Quit"); // Quit sub item
	    private JMenu mnuHelp = new JMenu("Help"); // Help Menu entry
	    private JMenuItem mnuItemAbout = new JMenuItem("About"); // About Entry

	    /** Constructor for the GUI */
	    

	    public GUI(){
			// Set menubar
	        f1.setJMenuBar(mb);

	        //Build Menus
	        mnuFile.add(mnuItemQuit);  // Create Quit line
	        mnuHelp.add(mnuItemAbout); // Create About line
	        mb.add(mnuFile);        // Add Menu items to form
	        mb.add(mnuHelp);

	        // Add Buttons
	        pnlNorth.setName("North");
	        pnlSouth.setName("South");
	        pnlEast.setName("East");
	        pnlWest.setName("West");
	        pnlCenter.setName("Center");
	        
	        pnlNorth.add(btnNorth);
	        pnlNorth.add(txt1);
	        pnlSouth.add(btnSouth);
	        pnlEast.add(btnEast);
	        pnlWest.add(btnWest);
	        pnlCenter.add(btnCenter);
	        pnlCenter.add(btnEast);

	        // Setup Main Frame
	        f1.getContentPane().setLayout(new BorderLayout());
			f1.getContentPane().add(pnlNorth, BorderLayout.NORTH);
			f1.getContentPane().add(pnlSouth, BorderLayout.SOUTH);
	        f1.getContentPane().add(pnlEast, BorderLayout.EAST);
			f1.getContentPane().add(pnlWest, BorderLayout.WEST);
	        f1.getContentPane().add(pnlCenter, BorderLayout.CENTER);
	    }


		
	    @SuppressWarnings("deprecation")
		public void launchFrame(){
	        // Display Frame
			f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	        f1.pack(); //Adjusts panel to components for display
	        f1.setVisible(true);
	        f1.show();
	       
	    }
	    
	}

	
	
}
